package com.rabbit.controller;

import com.github.pagehelper.PageInfo;
import com.rabbit.annotation.Log;
import com.rabbit.constant.OperType;
import com.rabbit.service.FileService;
import com.rabbit.util.ResultGenUtil;
import com.rabbit.validate.base.Delete;
import com.rabbit.vo.ResultVO;
import com.rabbit.vo.req.FileReq;
import com.rabbit.vo.res.FileRes;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <h1>文件中心</h1>
 * <p>
 * Created by zjw on 2022/2/15 9:57 下午
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 文件列表
     */
    @PostMapping("/list")
    public ResultVO<Object> list(@RequestBody FileReq fileReq) {
        PageInfo<FileRes> pageInfo = fileService.queryFiles(fileReq);
        return ResultGenUtil.genSuccess(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 删除文件
     */
    @PostMapping("/delete")
    @Log(name = "文件中心", type = OperType.DELETE)
    public ResultVO<Object> deleteFile(@Validated @RequestBody FileReq fileReq) {
        fileService.deleteFile(fileReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 下载文件
     */
    @GetMapping("/download")
    public void downloadFile(@RequestParam("fileId") Long fileId, HttpServletResponse response) {
        fileService.downloadFile(fileId, response);
    }
}